﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _1stVATCWin
{
    public partial class Shopping_Cart : Form
    {
        public Shopping_Cart()
        {
            InitializeComponent();
        }
        private void Load_dataGridView1()
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            String connectionstring = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from PHIEUXUAT";
            SqlCommand cmd = new SqlCommand(sql, conn);

            //C1: goi = data reader va luc celldoubleclick no se cong don du lieu
            //conn.Open();
            //SqlDataReader r = cmd.ExecuteReader();
            //while (r.Read())
            //{
            //    dataGridView1.Rows.Add(new string[] { r["publisherId"].ToString(), (string)r["publisherName"], (string)r["email"] });
            //}
            //r.Close();
            //conn.Close();

            //C2: goi = data set va luc celldoubleclick no se ko cong don du lieu
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Table PHIEUXUAT");
            DataTable dt = ds.Tables["Table PHIEUXUAT"];
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ds.Tables["Table PHIEUXUAT"].DefaultView;
        }

        private void Shopping_Art_Load(object sender, EventArgs e)
        {
            Load_dataGridView1();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
            {
                int i = dataGridView1.SelectedRows[0].Index;
                string id = dataGridView1.Rows[i].Cells["bookId"].Value.ToString();

                Config file = new Config(Application.StartupPath + "\\config.ini");
                String connectionstring = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "delete from PHIEUXUAT where IDphieuxuat = @IDphieuxuat";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@IDphieuxuat", id);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                dataGridView1.Rows.RemoveAt(i);
            }
            else
                {
                    MessageBox.Show("Dữ liệu Trống", "Thông Báo", MessageBoxButtons.OK);
                }  
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(dataGridView1.Rows.Count == 0)
            {
                MessageBox.Show("Dữ liệu Trống", "Thông Báo", MessageBoxButtons.OK);
            }
            else
            {
                int i = dataGridView1.SelectedRows[0].Index;
                string id = dataGridView1.Rows[i].Cells["bookId"].Value.ToString();

                Config file = new Config(Application.StartupPath + "\\config.ini");
                String connectionstring = file.ReadValue("Server", "conn"); // doc gia tri trong file config.ini tai truong [server], gia tri muon doc la con
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "delete from PHIEUXUAT where IDphieuxuat = @IDphieuxuat";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@IDphieuxuat", id);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                dataGridView1.Rows.RemoveAt(i);
            }    
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
                deleteToolStripMenuItem.Enabled = false;
        }
    }
}
